package com.fyzk.activity.zhifu;

import android.content.Intent;
import android.os.Bundle;

import com.fyzk.activity.app.ActivityTaskManager;
import com.fyzk.base.BaseActivity;
import com.fyzk.activity.app.AppToast;
import com.fyzk.activity.chongzhi.RechargeActivity;
import com.fyzk.activity.user.LoginActivity;
import com.fyzk.data.AppLotteryInfos;
import com.fyzk.data.preference.MySharedPreferences;
import com.fyzk.dialog.DJ_ProgressDialog;
import com.fyzk.dialog.SureDialog;
import com.fyzk.dialog.SureDialog.OnDialogOKCancleClickListener;
import com.fyzk.view.ViewCallBack;
import com.tyjc.lottery.R;
import com.fyzk.model.UMengTongJi_eventIds;
import com.fyzk.net.Commands;
import com.fyzk.utils.L;

import org.json.JSONException;
import org.json.JSONObject;

import java.math.BigDecimal;

import http.Common.CommandUtil;

public abstract class ZhifuNoLayoutBaseActivity extends BaseActivity {

	public int lotteryId;
	public String qiHao;
	protected boolean isPrizeStop = false;
	protected boolean isZhuijia = false;//是否追加投注（大乐透）
	/**
	 * dialog 对话框
	 */
	private SureDialog myDialog_ok = null;
	/**
	 * 彩金不足提醒对话框
	 */
	protected SureDialog leftMoneyLowDialog = null;
	/**
	 * 确认支付对话框
	 */
	protected SureDialog sureBuyDialog = null;

	/**
	 * 时间截止提示对话框
	 */
	public SureDialog endTimeDialog = null;

//	protected UserCommonBettingResponseData responseObj;
//	protected ZhuiHaoBettingResponseData responseObj_zhuiHao;
//	public FundsDetailResponseData accountObj;
	public String tips = "";
//	public CommonResult commonMsg;

	protected int beishu = 1;
	protected int zhuiQi = 1;
	protected int zhuiQi_GaoJi = 1;
	protected String phases = "";
	protected String beiShus = "";

	protected boolean isGaoJiZhuiHao;

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		lotteryId = getLotteryId();
		initDialogs();
	}

	public void initDialogs() {
//		myDialog_ok = new SureDialog(this, R.style.MyDialog);
//		leftMoneyLowDialog = new SureDialog(this, R.style.MyDialog);
//		sureBuyDialog = new SureDialog(this, R.style.MyDialog);
//		if (endTimeDialog == null) {
//			endTimeDialog = new SureDialog(getContext(), R.style.MyDialog);
//		}
	}
	
	public void lingAert() {
		AppToast.getToast().show(R.string.touyizhu);
	}
	/**
	 * 清空
	 */
	protected abstract void clear();
	
	protected abstract void toXiangQing();
	
	OnDialogOKCancleClickListener sureDialogClickListner = new OnDialogOKCancleClickListener() {

		@Override
		public void onOKClick() {
			// TODO Auto-generated method stub
			myDialog_ok.dismiss();
			clear();
			toXiangQing();
			finish();
		}

		@Override
		public void onCancleClick() {
			// TODO Auto-generated method stub
			myDialog_ok.dismiss();
			clear();
			finish();
		}

	};
	
	/**
	 * 判断彩金是否充足
	 */
	public boolean yuEisFill() {
		BigDecimal zhifu = getMoney();
		double leftMoney = 0;
		try {
			leftMoney = Double.parseDouble(MySharedPreferences.getInstance().getBalance().toString());
		} catch (Exception e) {

		}
		if (new BigDecimal(leftMoney) .compareTo(zhifu) < 0) {
			return false;
		}
		return true;
	}
	
	public void zhifuAction() {
		if (MySharedPreferences.getInstance().getLoginStatus()) {
			if (getZhushu() == 0) {
				lingAert();
			} else {
				UMengTongJi_eventIds.BuyOnClick(this, "认购", "" + lotteryId,AppLotteryInfos.getLotteryName(lotteryId));
				if (yuEisFill()) {
					if (AppLotteryInfos.getPeriodNew(lotteryId).equals("--")) {
						showEndTimeDialog("", "--");
					} else {
						showSureBuyDialog();
					}
				} else {
					showLeftMoneyLowDialog();
				}
			}
		} else {
			AppLotteryInfos.clear();
			ViewCallBack.getInstance().countDownObjs.clear();
			ActivityTaskManager.getInstance().closeAllActivity();
			Intent intent = new Intent(getContext(), LoginActivity.class);
			startActivity(intent);
		}
	}
	

	/**
	 * 时间截止提示对话框
	 */
	public void showEndTimeDialog(String period, String periodNew) {
		AppLotteryInfos.period_buffer.append(period + lotteryId);
		try {
			if(endTimeDialog == null){
				OnDialogOKCancleClickListener clickListener =  new OnDialogOKCancleClickListener() {

					@Override
					public void onOKClick() {
						endTimeDialog.dismiss();
					}

					@Override
					public void onCancleClick() {
						// TODO Auto-generated method stub
						endTimeDialog.dismiss();
					}
				};

				if (periodNew.equals("")) {

					endTimeDialog = new SureDialog(this,clickListener,"","第" + period + "期已截止！","确定","");
				} else {
					endTimeDialog = new SureDialog(this,clickListener,"","正在获取新期...","确定","");
				}
			}

			endTimeDialog.show();
		} catch (Exception e) {
			L.i(TAG, e.getMessage());
		}
	}


	public void setLeftMoney(){
	}


	/**
	 * 显示彩金不足对话框
	 */
	public void showLeftMoneyLowDialog() {

		if(leftMoneyLowDialog == null){
			OnDialogOKCancleClickListener dialogOKCancleClickListener = new OnDialogOKCancleClickListener() {

				@Override
				public void onOKClick() {
					Intent intent = new Intent(getContext(), RechargeActivity.class);
					startActivity(intent);
					leftMoneyLowDialog.dismiss();
				}

				@Override
				public void onCancleClick() {
					// TODO Auto-generated method stub
					leftMoneyLowDialog.dismiss();
				}
			};
			leftMoneyLowDialog = new SureDialog(this,dialogOKCancleClickListener,"","您的彩金不足！","充值","取消");
		}
		leftMoneyLowDialog.show();
	}

	/**
	 * 确认支付对话框
	 */
	public void showSureBuyDialog() {

		if(sureBuyDialog == null){
			OnDialogOKCancleClickListener listener =  new OnDialogOKCancleClickListener() {

				@Override
				public void onOKClick() {

					if (zhuiQi > 1) {
//						requestData(SchemaDef.ZHUIHAO_FAQI);
					} else {
						sureBuyDialog.dismiss();
						DJ_ProgressDialog.show(getContext(), true, true);
						bet();
					}


				}

				@Override
				public void onCancleClick() {
					// TODO Auto-generated method stub
					sureBuyDialog.dismiss();
				}
			};

			sureBuyDialog = new SureDialog(this,listener,"您的彩金为：" + MySharedPreferences.getInstance().getBalance()
					+ "米","支付：" + getMoney() + "米","支付","取消");

		}

		sureBuyDialog.show();
	}



	protected void bet() {
		try {
			JSONObject params = new JSONObject();
			params.put("betcode", getBetCode());
			params.put("lotteryType", lotteryId);
			params.put("phase", getPeriod());
			params.put("amount", getOneAmount() * getBeishu() * 100 * getZhushu());
			params.put("multiple", getBeishu());
			params.put("oneAmount", getOneAmount() * 100);
			params.put("prizeOptimize", "0");//prizeOptimize奖金诱惑（优化：1；普通：0）
			params.put("codeFilter", "1");//codeFilter旋转过滤（过滤：1；普通：0）
			params.put("userCouponId", "");//优惠劵Id，非优惠劵投注可以不写
			httpPostJson(CommandUtil.USER_COMMON_BETTING, Commands.BET, params, false);
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}



	/**
	 * 获取注数
	 */
	public abstract int getZhushu();

	/**
	 * 单倍彩金(米)
	 * 
	 * @return
	 */
	public int getAmt() {
		return getZhushu() * getOneAmount();
	}

	public String getLotteryType(){
		return lotteryId+"";
	}
	public String getCodeFilter(){
		return "0";
	}
	
	public int getBeishu() {

		return beishu;
	}

	public int getZhuiQi() {
		return zhuiQi;
	}

	public String getBetCode() {
		return "";
	};

	

	/**
	 * 单注彩金(米)
	 * 
	 * @return
	 */
	public abstract int getOneAmount();

	/**
	 * 投注彩金(米)
	 * 
	 * @return
	 */
	public BigDecimal getMoney() {
		if (!beiShus.equals("") && beiShus.contains(",")) {
			BigDecimal money = new  BigDecimal(getBeishu()).multiply(new  BigDecimal(getZhushu())).multiply(new BigDecimal(getOneAmount()));
			String[] beis = beiShus.split(",");
			for (int i = 1; i < beis.length; i++) {
				int b = 1;
				try {
					b = Integer.parseInt(beis[i]);
				} catch (Exception e) {
					// TODO: handle exception
				}
				money = money.add((new BigDecimal(b).multiply(new BigDecimal(getZhushu()))).multiply(new BigDecimal(getOneAmount()))) ;
			}
			return money;
		} else {
			return new BigDecimal(getBeishu()).multiply(new BigDecimal(getZhushu())).multiply(new BigDecimal(getOneAmount())).multiply(new BigDecimal(getZhuiQi()));
		}
	}


	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		qiHao = getPeriod();
		if (MySharedPreferences.getInstance().getLoginStatus()) {
			DJ_ProgressDialog.show(getContext(), true, true);
//			requestData(SchemaDef.ACCOUNT_MONEY);
		}
	}

	public String getPeriod() {
		return AppLotteryInfos.getPeriod(lotteryId);
	}

	public abstract int getLotteryId();
}
